/**
 * 
 */
package com.srini.servlets;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.srini.pojos.LoggedInUser;
import com.srini.reports.ReimbursementReport;
import com.srini.util.AppUtil;
import com.srini.util.ConnectionUtil;
import com.srini.util.Constants;
import com.srini.util.LeaveHolyDays;
import com.srini.util.TRMSUtil;

/**
 * @author Sriniz
 * 
 */
public class ReimbursementServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	String[] months = { "", "January", "February", "March", "April", "May",
			"June", "July", "August", "September", "October", "November",
			"December" };

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("Reimbursement Details.....");

		resp.setContentType("application/vnd.ms-excel");

		LoggedInUser user = (LoggedInUser) req.getSession().getAttribute(
				Constants.LOGGEDIN_USER);
		if (user == null) {
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		String userName = user.getUserInfo().getFullName(), fileName = "";

		if (AppUtil.isEmpty(userName)) {
			userName = user.getUserName();
		}
		/*
		 * if (!AppUtil.isEmpty(user.getUserInfo().getVehicle()) &&
		 * user.getUserInfo().getVehicle().equals("2")) { String message =
		 * "Error::You are not eligible for reimbursement.";
		 * req.setAttribute("message",message);
		 * req.getRequestDispatcher("/main.jsp").forward(req, resp); return; }
		 */
		if (AppUtil.isEmpty(user.getUserInfo().getDistanceFromHome())
				|| AppUtil.isEmpty(user.getUserInfo().getVehicle())
				&& !user.getUserInfo().getVehicle().equals("2")) {
			String message = "Error::You need to fill your details before generating reports.";
			req.setAttribute("message", message);
			req.setAttribute("currentPage", "userprofile");
			req.getRequestDispatcher("/home.jsp").forward(req, resp);
			return;
		}
		String month = req.getParameter("month");
		int formType = Integer.parseInt(req.getParameter("formtype"));

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		cal.set(Calendar.DATE, 1);
		Map days = LeaveHolyDays.getHolidays(user.getId(),
				new ConnectionUtil().getConnection());
		HSSFWorkbook workBook = null;
		if (formType == 1) {
			workBook = TRMSUtil.getInstance().generateReimbursementForm(user,
					cal, days);
			fileName = userName + "_Reimbursement_Details_"
					+ months[Integer.parseInt(month)] + ".xls";
		} else {
			workBook = TRMSUtil.getInstance().generateCashVoucher(user, cal,
					days);
			fileName = userName + "_Cash_Voucher_"
					+ months[Integer.parseInt(month)] + ".xls";
		}
		// Write to the Report tables
		ReimbursementReport report = new ReimbursementReport();
		report.setUserId(user.getId());

		// Write the output
		OutputStream out = resp.getOutputStream();
		resp.setHeader("Content-Disposition", "attachment; filename="
				+ fileName);
		workBook.write(out);
		out.close();
	}

}
